package com.xiaowei.gym.gymjsp.ware.dao.impl;

import com.mysql.cj.util.StringUtils;
import com.xiaowei.gym.gymjsp.ware.dao.AttendanceDao;
import com.xiaowei.gym.gymjsp.ware.entity.AdminEntity;
import com.xiaowei.gym.gymjsp.ware.entity.AttendanceEntity;
import com.xiaowei.gym.gymjsp.ware.util.JdbcUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AttendanceDaoImpl implements AttendanceDao {
    @Override
    public int getCount() {
        String sql = "SELECT count(1) from gym_attendance";
        int i = 0;
        try {
            i = JdbcUtil.executeQuery(sql, rs -> {
                int counts = 0;
                if (rs.next()) {
                    counts = rs.getInt("count(1)");
                }
                return counts;
            }, null);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public List<AttendanceEntity> getList(String employeeId, int currentPageNo, Integer pageSize) {
        List<AttendanceEntity> attendanceEntities = new ArrayList<>();
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from gym_attendance ");
        List<Object> list = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(employeeId)){
            sql.append("where employee_id = ?");
            list.add(employeeId);
        }
        //在mysql数据库中，分页使用 limit startIndex，pageSize ; 总数
        sql.append(" order by id DESC limit ?,?");
        currentPageNo = (currentPageNo - 1) * pageSize;
        list.add(currentPageNo);
        list.add(pageSize);
        Object[] params = list.toArray();
        System.out.println("sql ----> " + sql.toString());
        try {
            List<AttendanceEntity> attendanceEntityList = new ArrayList<>();
            attendanceEntities = JdbcUtil.executeQuery(sql.toString(), rs -> {
                while (rs.next()) {
                    AttendanceEntity attendanceEntity = new AttendanceEntity();
                    attendanceEntity.setId(rs.getInt("id"));
                    attendanceEntity.setEmployee_id(rs.getInt("employee_id"));
                    attendanceEntity.setTime_in(rs.getTime("time_in"));
                    attendanceEntity.setTime_out(rs.getTime("time_out"));
                    attendanceEntityList.add(attendanceEntity);
                }
                return attendanceEntityList;
            }, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return attendanceEntities;
    }

    @Override
    public AttendanceEntity getById(String aid) {
        String sql = "SELECT * FROM gym_attendance WHERE id = ?";
        AttendanceEntity attendanceEntity = null;
        try {
            attendanceEntity = JdbcUtil.executeQuery(sql, new JdbcUtil.ResultSetHandler<AttendanceEntity>() {
                @Override
                public AttendanceEntity handle(ResultSet rs) throws SQLException {
                    AttendanceEntity attendanceEntity = new AttendanceEntity();
                    if (rs.next()) {
                        attendanceEntity.setId(rs.getInt("id"));
                        attendanceEntity.setEmployee_id(rs.getInt("employee_id"));
                        attendanceEntity.setTime_in(rs.getTimestamp("time_in"));
                        attendanceEntity.setTime_out(rs.getTimestamp("time_out"));
                    }
                    return attendanceEntity;
                }
            }, aid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return attendanceEntity;
    }

    @Override
    public int createAttendance(AttendanceEntity attendanceEntity) {
        StringBuffer sql = new StringBuffer();
        sql.append("INSERT INTO gym_attendance(employee_id,time_in,time_out)");
        sql.append(" values(?,?,?)");
        List<Object> list = new ArrayList<>();
        if (attendanceEntity.getEmployee_id() != null){
           list.add(attendanceEntity.getEmployee_id());
        }
        if (attendanceEntity.getTime_in() != null){
            list.add(attendanceEntity.getTime_in());
        }
        if (attendanceEntity.getTime_out() != null){
            list.add(attendanceEntity.getTime_out());
        }
        int i = 0;
        try {
            i = JdbcUtil.executeUpdate(sql.toString(), list.toArray());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }

    @Override
    public int updateAttendance(AttendanceEntity attendanceEntity) {

        String sql = "UPDATE gym_attendance\n" +
                "SET time_in = ? , time_out = ?\n" +
                "WHERE id = ?\n";
        Object[] params = {attendanceEntity.getTime_in(),attendanceEntity.getTime_out(),attendanceEntity.getId()};
        int i = 0;
        try {
             i = JdbcUtil.executeUpdate(sql, params);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return i;
    }
}
